/*
@Author   : Uyynot
@Email    : uyynot@qq.com
@Time     : 2023/8/24 14:54
@File     : list.go
@Project  : web_scaff
@Desc     :
*/
package user

import (
	"web_scaff/configs"
	"web_scaff/models"

	"gorm.io/gorm"
)

type WhereData struct {
	Page     int    `form:"page"`      // 第几页
	PageSize int    `form:"page_size"` // 每页显示条数
	Username string `form:"username"`  // 用户名
	Phone    string `form:"phone"`     // 手机号
}

func List(p *WhereData) (total int64, users []models.User, err error) {
	if err = configs.Db.Model(&models.User{}).Count(&total).Error; err != nil {
		return
	}
	queryDb := configs.Db.Debug().Order("id desc").Limit(p.PageSize).Offset((p.Page - 1) * p.PageSize).Session(&gorm.Session{})
	if p.Username != "" {
		queryDb = queryDb.Where("username =?", p.Username)
	}
	if p.Phone != "" {
		queryDb = queryDb.Where("phone =?", p.Phone)
	}
	err = queryDb.Find(&users).Error
	return
}
